package com.school.zhaoban.controller;

import com.school.zhaoban.pojo.Login;
import com.school.zhaoban.response.CommonResponse;
import com.school.zhaoban.utils.UserLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2022/1/9 17:27
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */

@Slf4j
@Api(tags = "登录类")
@RestController
//@CrossOrigin
public class LoginController {


    @UserLoginToken
    @ApiOperation("用户登录")
    @PostMapping(value = "/login")
    public CommonResponse Login(@RequestBody Login login){
        System.out.println(login.getPassword());
        String token = login.getUsername();
        System.out.println(token);
        return CommonResponse.ok(token + "-accessToken");
    }

    @ApiOperation("用户信息")
    @PostMapping("/userInfo")
    public CommonResponse info(@RequestBody String token){
        System.out.println(token);
        String tokenByRole = token.substring(1, token.indexOf('-'));
        ArrayList<String> list = new ArrayList<String>();
        log.info(tokenByRole);
        list.add(tokenByRole);

        HashMap<String, Object> hash = new HashMap<>();
        hash.put("permissions",list);
        hash.put("avatar","https://img0.baidu.com/it/u=3623309039,2407011632&fm=26&fmt=auto");
        return CommonResponse.ok(hash);
    }


    @ApiOperation("用户退出")
    @GetMapping("/logout")
    public CommonResponse logout(){
        return CommonResponse.ok("admin");
    }


    @ApiOperation("密钥")
    @PostMapping("/publicKey")
    public CommonResponse Key(){
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("publicKey", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBT2vr+dhZElF73FJ6xiP181txKWUSNLPQQlid6DUJhGAOZblluafIdLmnUyKE8mMHhT3R+Ib3ssZcJku6Hn72yHYj/qPkCGFv0eFo7G+GJfDIUeDyalBN0QsuiE/XzPHJBuJDfRArOiWvH0BXOv5kpeXSXM8yTt5Na1jAYSiQ/wIDAQAB");
        return CommonResponse.ok(hash);
    }



}
